package com.niubility.server.Impl;

import com.niubility.dao.PwdDao;
import com.niubility.entity.Pwd;
import com.niubility.server.IPwdService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PwdService implements IPwdService {
    @Resource
    private PwdDao pwdDao;

    //加载所有用户
    public List<Pwd> LoadAll() {
        return pwdDao.LoadAll();
    }

    //通过Id查询某一个用户
    public Pwd LoadById(Integer userId) {
        return pwdDao.LoadById(userId);
    }

    //添加用户
    public boolean Save(Pwd pwd) {
       return pwdDao.Save(pwd);
    }

    //根据id删除用户
    public boolean Delete(Integer userId) {
        return pwdDao.Delete(userId);
    }

    //删除多个用户
    public boolean deleteUserByIds(Integer[] userIds) {
        return pwdDao.deleteUserByIds(userIds);
    }

    //更新用户信息
    public boolean Update(Pwd pwd) {
        return pwdDao.Update(pwd);
    }

    //SHA1密码加密
    public String CipherEncryptionBySHA1(String credentials,String userTel){
        String hashAlgorithmName = "SHA1";
        Object salt = ByteSource.Util.bytes(userTel);
        int hashIterations = 1024;
        Object result= new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
        return result.toString();
    }

    //MD5密码加密
    public String CipherEncryptionByMD5(String credentials, String userTel) {
        String hashAlgorithmName = "MD5";
        Object salt = ByteSource.Util.bytes(userTel);
        int hashIterations = 1024;
        Object result= new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
        return result.toString();
    }

    //用户注册加入密码表
    public boolean DepRig(Pwd pwd, String depTel) {
        String userPassword  = pwd.getUserPwd();
        int userId = pwd.getUserId();
        String userPwd = CipherEncryptionBySHA1(userPassword,depTel);
        Pwd pwd1 = new Pwd();
        pwd1.setUserId(userId);
        pwd1.setUserPwd(userPwd);
        return Save(pwd1);
    }
















}
